package com.fzh.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fzh.mapper.UserMapper;
import com.fzh.pojo.entity.User;
import com.fzh.pojo.utils.ResponeMessageUtil;
import com.fzh.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public User backLogin(String name,String password) {
		return userMapper.backLogin(name, password);
	}

	@Override
	public List<User> queryAllUser(int setValue,int limit,String name,String gender,String isAdmin) {
		return userMapper.selectAllUser(setValue,limit,name,gender,isAdmin);
	}

	@Override
	public int queryUserCounts(String name,String gender,String isAdmin) {
		return userMapper.selectUserCounts(name,gender,isAdmin);
	}

	@Override
	public Boolean addUser(User user) {
		return ResponeMessageUtil.getResMsg(userMapper.insertUser(user));
	}

	@Override
	public Boolean editUserById(User user) {
		return ResponeMessageUtil.getResMsg(userMapper.updateUserById(user));
	}

	@Override
	public Boolean delAllUserByIds(String id) {
		String[] split = id.split(",");
		List<String> ids = new ArrayList<>();
		for(int i = 0; i < split.length; i ++) {
			ids.add(split[i]);
		}
		return ResponeMessageUtil.getResMsg(userMapper.deleteAllUserByIds(ids));
	}

	@Override
	public Boolean delUserById(int id) {
		return ResponeMessageUtil.getResMsg(userMapper.deleteUserById(id));
	}

	@Override
	public void editUserLastLoginTimeById(Integer id) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String date = dateFormat.format(new Date());
		userMapper.updateUserLastLoginTimeById(date,id);
	}

	@Override
	public User queryUserByNameVerifyError(String name) {
		return userMapper.selectUserByNameVerifyError(name);
	}

	@Override
	public Boolean editUserIsLockById(Integer id, Integer number) {
		return ResponeMessageUtil.getResMsg(userMapper.updateUserIsLockById(id,number));
	}

	@Override
	public Boolean editUserAdminStatusById(Integer id, Integer number) {
		return ResponeMessageUtil.getResMsg(userMapper.updateUserAdminStatusById(id,number));
	}

	@Override
	public List<User> queryAllUserWitchOrderIndoSelect() {
		return userMapper.selectAllUserWitchOrderIndoSelect();
	}

	@Override
	public void eidtUserErrorCountById(Integer id, int errorCount, String datestr) {
		userMapper.updateUserErrorCountById(id,errorCount,datestr);
	}

	@Override
	public Boolean editUserBasicInformationById(User user) {
		return ResponeMessageUtil.getResMsg(userMapper.updateUserBasicInformationById(user));
	}

	@Override
	public Boolean queryAllUserByNameWitchVerify(String newName) {
		List<User> users = userMapper.selectAllUserByNameWitchVerify(newName);
		if(users.size() > 0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public User frontLogin(String name, String password) {
		return userMapper.frontLogin(name,password);
	}
	
	@Override
	public User selectLogins(Integer id, String password) {
		return userMapper.selectLogins(id, password);
	}
	
	@Override
	public User selectByuserId(Integer id) {
		return userMapper.selectByuserId(id);
	}
	
	@Override
	public int updatePassWord(Integer id, String password) {
		return userMapper.updatePassWord(id, password);
	}

	@Override
	public int updateUserById(User user) {
		return userMapper.updateUserById(user);
	}

}
